﻿#include "DeviceWidget.h"
#include "ui_DeviceWidget.h"
#include "CommonFunc.h"
#include "DataInfo.h"
#include <QToolButton>
#include <QGridLayout>
#include <QSpacerItem>

DeviceWidget::DeviceWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DeviceWidget)
{
    ui->setupUi(this);

    _gridLayout = new QGridLayout();
    _gridLayout->setSpacing(30);
    _gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    _gridLayout->setContentsMargins(30, 50, 30, 50);
    _gridLayout->setHorizontalSpacing(40);
    _gridLayout->setVerticalSpacing(40);
    this->setLayout(_gridLayout);

    _verticalSpacer = new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding);
//    gridLayout->addItem(verticalSpacer, 6, 1, 1, 1);

    _horizontalSpacer = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
//    gridLayout->addItem(horizontalSpacer, 5, 1, 1, 3);
}

DeviceWidget::~DeviceWidget()
{
    delete ui;
}

void DeviceWidget::clearAndShowDeviceInfo()
{
    _gridLayout->removeItem(_horizontalSpacer);
    _gridLayout->removeItem(_verticalSpacer);

    for (int i = 0; i < _vecButton.size(); ++i)
    {
        _gridLayout->removeWidget(_vecButton[i]);
        _vecButton[i]->hide();
        _vecButton[i]->deleteLater();
    }
    _vecButton.clear();

    if (g_mapGroupIdToListSensor.empty())
    {
        return;
    }

    int iCurNum = -1;

    QMapIterator<int, QList<Sensor> > iter(g_mapGroupIdToListSensor);
    while (iter.hasNext()) {
        iter.next();
        int gID = iter.key();
        for (int i = 0; i < iter.value().size(); ++i)
        {
            ++iCurNum;
            const Sensor& ss = iter.value()[i];

            QString strName = QString("%1%2").arg(gID).arg(ss.cell_code);

            QToolButton *toolButton = new QToolButton(this);
            toolButton->setObjectName(QString::fromUtf8("toolButton"));
//            toolButton->setMinimumSize(QSize(50, 30));
            QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            sizePolicy.setHorizontalStretch(0);
            sizePolicy.setVerticalStretch(0);
            sizePolicy.setHeightForWidth(toolButton->sizePolicy().hasHeightForWidth());
            toolButton->setSizePolicy(sizePolicy);
            QFont font1;
            font1.setPointSize(18);
//            font1.setBold(true);
            toolButton->setFont(font1);
            toolButton->setFixedHeight(90);
//            toolButton->setMinimumHeight(90);
            toolButton->setMaximumWidth(330);
            toolButton->setText(strName);
            if (ss.status == "stop")
            {
                toolButton->setStyleSheet(
                            "QToolButton{border:none; outline:none; color:white;  \
                            background-color:qlineargradient(x1: 0, y1: 0.5, x2: 1, y2: 0.5,stop: 0 #ED213A, stop: 1.0 #93291E); }");
            }
            else if (ss.status == "slow")
            {
                toolButton->setStyleSheet(
                            "QToolButton{border:none; outline:none; color:white;  \
                            background-color:qlineargradient(x1: 0, y1: 0.5, x2: 1, y2: 0.5,stop: 0 #FDC830, stop: 1.0 #F37335); }");
            }
            else {
                toolButton->setStyleSheet(
                            "QToolButton{border:none; outline:none; color:white ; \
                            background-color:qlineargradient(x1: 0, y1: 0.5, x2: 1, y2: 0.5,stop: 0 #11998e, stop: 1.0 #38ef7d); }");
            }
            _gridLayout->addWidget(toolButton, iCurNum / _iColumn, iCurNum % _iColumn, 1, 1);

            _vecButton.push_back(toolButton);
        }
    }
    int iYu = iCurNum % _iColumn;
    if (iYu != 0)
    {
        _gridLayout->addItem(_horizontalSpacer, iCurNum / _iColumn, iYu, 1, _iColumn - iYu);
    }

    _gridLayout->addItem(_verticalSpacer, iCurNum / _iColumn +1, 0, 1, 1);
}
